<?php
defined('_JEXEC') or die('Restricted access');

class latestnewsHelper
{
	function getContent($params)
	{
		$count		= (int) $params['count'];
		$catid		= trim( $params['catid'] );
		$secid		= trim( $params['secid'] );
		$show_front	= $params['show_front'];
		
		switch ($params['ordering'])
		{
			case 'm_dsc':
				$ordering		= 'c.modified DESC, c.created DESC';
				break;
				
			case 'hits':
				$ordering		= 'c.hits DESC';
				break;
				
			case 'c_dsc':
			default:
				$ordering		= 'c.created DESC';
				break;
		}

		if ($catid)
		{
			$ids = explode( ',', $catid );
			$catCondition = ' AND (c.catid=' . implode( ' OR c.catid=', $ids ) . ')';
		}
		if ($secid)
		{
			$ids = explode( ',', $secid );
			$secCondition = ' AND (c.sectionid=' . implode( ' OR c.sectionid=', $ids ) . ')';
		}
		
		$query='select c.*, cat.title AS cat_title, cat.alias AS cat_alias from dm_content AS c, dm_categories AS cat where c.catid=cat.id AND c.state = 1 '.
				($catid ? $catCondition : '').
				($secid ? $secCondition : '').
				' ORDER BY '. $ordering;
		$rows=mysql_query($query);
		$i		= 0;
		$lists	= array();
		while($row=mysql_fetch_assoc($rows))
		{
			$img_url="";
			$introtext=$row['introtext'];
			$find_img=strpos($introtext,"img");
			if ($find_img){
			  $end_img=strpos($introtext,"src=",$find_img);
			  if ($end_img){
				$j=$end_img;
				while (($introtext[$j]!='"')||($j<count($introtext))){
				  $img_url.=$introtext[$j];
				  $j++;
				  if ($img_url=="src="){
					$img_url="";
					$j++;
				  }
				}
			  } 
			} 
			  $lists[$i]->hinhanh= $img_url;
			  $lists[$i]->introtext= strip_tags($row['introtext']);
			  $lists[$i]->title = htmlspecialchars( $row['title'] );
			  $lists[$i]->link =BASE_URL.'/'.$row['cat_alias'].'/'.$row['alias'].'-'.$row['id'].'.html';
			  $i++;
		}
		return  $lists;	
	}
}
